package com.itxg.booksmanage.config;

import com.itxg.booksmanage.filter.AuthHandlerInterceptor;
import com.itxg.booksmanage.filter.LoginFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.Arrays;


/**
 * 注册拦截器并添加配置
 *
 * @author Administrator
 */
@Configuration
public class MvcConfigurer implements WebMvcConfigurer {


    /**
     * 配置登录、注册过滤器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 使用登录拦截器
//        registry.addInterceptor()
        // 使用 Token 拦截器 -- 有值 new AuthHandlerInterceptor()
        registry.addInterceptor(new LoginFilter())
                .excludePathPatterns(Arrays.asList("/test/test-token", "/test/login",
                        "/user/login", "/user/regist", "/user/logout", "/book/all")) // 排除登录、注册地址
                .addPathPatterns("/**"); // 拦截所有


    }


    /**
     * 配置允许所有的接口跨域访问
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}

